import 'package:http/http.dart' as http;

class CookieManager {
  static final CookieManager _instance = CookieManager._internal();
  Map<String, String> _cookies = {};

  factory CookieManager() {
    return _instance;
  }

  CookieManager._internal();

  void setCookies(http.Response response) {
    String? allSetCookie = response.headers['set-cookie'];
    if (allSetCookie != null) {
      List<String> cookies = allSetCookie.split(',');
      for (var cookie in cookies) {
        List<String> cookieParts = cookie.split(';');
        for (var part in cookieParts) {
          List<String> keyValue = part.split('=');
          if (keyValue.length == 2) {
            _cookies[keyValue[0].trim()] = keyValue[1].trim();
          }
        }
      }
    }
  }

  Map<String, String> get cookies => _cookies;

  void clearCookies() {
    _cookies.clear();
  }
}